package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Building;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.service.BuildingService;
import org.example.studentsystem_back.mapper.BuildingMapper;
import org.example.studentsystem_back.vo.buildingMangeVo.BuildingIdVo;
import org.example.studentsystem_back.vo.buildingMangeVo.BuildingVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 马鑫
* @description 针对表【building】的数据库操作Service实现
* @createDate 2024-07-12 09:30:32
*/
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building>
    implements BuildingService{
    @Resource
    private BuildingMapper buildingMapper;

    @Override
    public CommonResponse<BuildingVo> findAll(PageDTO pageDTO, String id) {
        Page<Building> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("building_name",id);
        queryWrapper.orderByAsc("area");
        queryWrapper.orderByAsc("building_name");
        List<Building> buildings=buildingMapper.selectPage(page,queryWrapper).getRecords();
        List<BuildingIdVo> buildingIdVos=new ArrayList<>();
        int i=pageDTO.getPage()*10-9;
        for(Building building:buildings){
            BuildingIdVo buildingIdVo=new BuildingIdVo();
            buildingIdVo.setSuiId(i);
            buildingIdVo.setBuildingIdVo(building);
            buildingIdVos.add(buildingIdVo);
            i++;
        }

        BuildingVo buildingVo=new BuildingVo();
        buildingVo.setBuildingIdVos(buildingIdVos);
        int total= Math.toIntExact(buildingMapper.selectCount(queryWrapper));
        buildingVo.setTotal(total);
        return CommonResponse.createForSuccess("查询成功",buildingVo);
    }

    @Override
    public CommonResponse<Building> add(Building building){
        buildingMapper.insert(building);
        return CommonResponse.createForSuccess("添加成功",building);
    }

    @Override
    public CommonResponse<Building> update(Building building){
        buildingMapper.updateById(building);
        return CommonResponse.createForSuccess("更新成功",building);
    }

    @Override
    public CommonResponse<Building> delete(Building building){
        buildingMapper.deleteById(building);
        return CommonResponse.createForSuccess();
    }
}




